
package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo.cache.service.IRedisService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

//定义监听器，实现接口：ApplicationListener<ContextRefreshedEvent>

@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;

    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private IRedisService redisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //当容器启动时就执行监听器
        System.out.println("---------------begin---------------------");
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO vo = null;
        for (StrategyDetail detail : list) {
            if (!redisService.exsit(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO, detail.getId() + "")) {

                vo = new StrategyStatisVO();
                vo.setIsabroad(detail.isIsabroad());
                vo.setDestId(detail.getDest().getId());
                vo.setDestName(detail.getDest().getName());
                vo.setTitle(detail.getTitle());
                vo.setStrategyId(detail.getId());
                vo.setThumbsupnum(detail.getThumbsupnum());
                vo.setSharenum(detail.getSharenum());
                vo.setReplynum(detail.getReplynum());
                vo.setFavornum(detail.getFavornum());
                vo.setViewnum(detail.getViewnum());
                strategyStatisRedisService.setStatisVo(vo);
                //初始化操作
                //初始化国内外排行
                int sortScore=detail.getFavornum()+detail.getThumbsupnum();
                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMOND_SORT, detail.getId(), sortScore);
                //初始化热门
                int hotScore = detail.getReplynum()+detail.getViewnum();
                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMOND_HOT, detail.getId(), hotScore);
            }

        }
        System.out.println("---------------end---------------------");
    }




}


//记得导入RedisConfig配置文件

//@Import(RedisConfig.class)

